<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Model\AuthGroupModel;
use Think\Page;
header("Content-Type: text/html;charset=utf-8");
/**
 * 后台内容控制器
 * @author huajie <banhuajie@163.com>
 */
class ManageController extends AdminController {
		//轮播列表
    public function banner_index(){
        $list=M('banner')->order('sort asc')->select();
        foreach($list as $k=>$v){
            if($v['img']){
                $list[$k]['img']=M('picture')->where("id=$v[img]")->getField('path');
            }else{
                $list[$k]['img']='';
            }
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);

        }
        $this->assign('list',$list);
        $this->display();
    }
    //添加轮播图
    public function banner_add(){
        $data=$_POST;
        if($_POST && empty($data['id'])){
            $upload = new \Think\Upload();
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $ii=$upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
            $uu=$upload->savePath ='Picture/'; // 设置附件上传（子）目录
            $info   =   $upload->upload();
            if($info['img']){
                $p=$info['img']['savepath'];
                $n=$info['img']['savename'];
                $imgurl['path']='/Uploads/'.$p.$n;
                $imgurl['status'] = 1;
                $imgurl['create_time'] = time();
                $picture_id = D('picture')->add($imgurl);
                $data['img']=$picture_id;                
            }
            $data['create_time']=time();
            $res=M('banner')->add($data);
            if($res){
                return $this->success('添加成功',U('banner_index'));
            }else{
                return $this->error('添加失败',U('banner_index'));
            }
        }elseif($data['id']){
            $upload = new \Think\Upload();
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $ii=$upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
            $uu=$upload->savePath ='Picture/'; // 设置附件上传（子）目录
            $info   =   $upload->upload();
            if($info['img']){
                $p=$info['img']['savepath'];
                $n=$info['img']['savename'];
                $imgurl['path']='/Uploads/'.$p.$n;
                $imgurl['status'] = 1;
                $imgurl['create_time'] = time();
                $picture_id = D('picture')->add($imgurl);
                $data['img']=$picture_id;                
            }
            $res=M('banner')->save($data);
            if($res){
                return $this->success('修改成功',U('banner_index'));
            }else{
                return $this->error('修改失败',U('banner_index'));
            }
        }
        $id=I('id');
        if($id){
            $info=M('banner')->where("id=$id")->find();
            $info['img']=M('picture')->where("id=$info[img]")->getfield('path');
        }
        $this->assign('info',$info);
        $this->display();
    }
    //删除轮播图
    public function banner_del(){
        $id_list=I('id');
        $id=explode(',',$id_list);
        foreach($id as $k=>$v){
            $res=M('banner')->where("id=$v")->delete();
        }
        if($res){
            $value=['info'=>'删除成功'];
        }else{
            $value=['info'=>'删除失败'];
        }
        echo json_encode($value);exit;
    }
    //楼盘列表
    public function floor_index(){

    	$this->display();
    }
    public function floor_add(){

    	$this->display();
    }
    public function floor_del(){

    }
    //置业顾问
    public function property_consultant(){
        $data=$_POST;
        $data['name']?$map['name']=array('like',"%$data[name]%"):'';
        $count=M('property_consultant')->where($map)->count();
        $Page      = new \Think\Page($count,10,$data);
        $show      = $Page->show();
        $list=M('property_consultant')->where($map)->order('sort asc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($list as $k=>$v){
            if($v['img']){
                $list[$k]['img']=M('picture')->where("id=$v[img]")->getField('path');
            }else{
                $list[$k]['img']='';
            }
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);

        }
        $this->assign('_page',$show);
        $this->assign('msg',$data);
        $this->assign('list',$list);
        $this->display();
    }
    //
    public function property_add(){

    }
    public function property_del(){

    }
    //围观人员
    public function onlookers(){

    }
    public function onlookers_add(){

    }
    public function onlookers_del(){

    }


    //机构管理
    public function organizational_management(){
        $map['is_del']=array('neq',1);
        $list=M('mechanism')->where($map)->select();
        foreach($list as $k=>$v){
            if($v['logo']){
                $list[$k]['logo']=M('picture')->where("id=$v[logo]")->getField('path');
            }else{
                $list[$k]['logo']='';
            }
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
        }
        // print_r($list);die;
        $this->assign('list',$list);
        $this->display();
    }
    //新增机构
    public function me_add(){
        if($_POST){
            if(!$_POST['phone']){
                $this->error('手机号码不能为空');
            }
            if(!$_POST['password']){
                $this->error('密码不能为空');
            }
            $data['password']=$this->think_ucenter_md5($_POST['password'], '@gBeM[(lqcLzSduvpOY}~KH-i6?G47W%V1wy+;5D');
            $data['phone']=$_POST['phone'];
            $data['create_time']=time();
            $res=M('mechanism')->add($data);
            if($res){
                return $this->success('成功',U('Manage/organizational_management'));
            }else{
                return $this->error('失败',U('Manage/organizational_management'));
            }
        }

        $this->display();
    }
    function think_ucenter_md5($str, $key = 'ThinkUCenter'){
        return '' === $str ? '' : md5(sha1($str) . $key);
    }
    //查看机构信息
    public function me_ck(){
        $id=I('id');
        $info=M('mechanism')->where("id=$id")->find();
        if($info['logo']){
            $info['logo']=M('picture')->where("id=$info[logo]")->getField('path');
        }
        if($info['id_card_z']){
            $info['id_card_z']=M('picture')->where("id=$info[id_card_z]")->getField('path');
        }
        if($info['id_card_f']){
            $info['id_card_f']=M('picture')->where("id=$info[id_card_f]")->getField('path');
        }
        if($info['business_license']){
            $info['business_license']=M('picture')->where("id=$info[business_license]")->getField('path');
        }
        
        $info['create_time']=date('Y-m-d H:i:s',$info['create_time']);
        // print_r($info);die;
        $this->assign('info',$info);
        $this->display();
    }
    //机构删除
    public function me_del(){
        $id=I('id');
        $res=M('mechanism')->where("id=$id")->save(['is_del'=>1]);

        if($res){
            $value=['info'=>'删除成功'];
        }else{
            $value=['info'=>'删除失败'];
        }
        echo json_encode($value);exit;
    }
}